package com.abc.robot01.vivemodel

import android.util.Log
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.abc.robot01.retrofit.RetrofitService
import com.abc.robot01.retrofit.bean.Commodity
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.time.delay

/**
 * 商品详情
 */
class XiangQingViewModel : BaseViewModel() {

    private lateinit var all: List<String>
    var index  = 0 ;

    // 定义一个 Map
    val categoryMap = mapOf(
        "HOME_DECORATION" to "家装建材",
        "FURNITURE" to "家居饰品",
        "TECHNOLOGY" to "技术服务"
    )

    val TAG = "XiangQingViewModel"

    val companyName = mutableStateOf("")

    /**
     * 商品关联详情长图
     */
    val detailImg = mutableStateOf("")

    // StateFlow to hold the commodity data
    private val _commodity = MutableStateFlow<Commodity?>(null)
    val commodity: StateFlow<Commodity?> get() = _commodity

    //多图片
    // StateFlow to hold the commodity data
    private val _images = MutableStateFlow<String?>(null)
    val images: StateFlow<String?> get() = _images

    val bigImg = MutableStateFlow<String?>(null)

    //品牌 ,显示在界面上。
    private val _brandList = MutableStateFlow<List<Pair<String, String>>>(emptyList())
    val brand: StateFlow<List<Pair<String, String>>> = _brandList

    //系列 ,显示在界面上。
    private val _seriesList = MutableStateFlow<List<Pair<String, String>>>(emptyList())
    val series: StateFlow<List<Pair<String, String>>> = _seriesList


    fun load(id: String) {
        viewModelScope.launch {
            try{
            val s2 = RetrofitService.instance.getLabelList(
                accessToken = Robot.accessToken,
                companyId = Robot.companyId,
                type = "COMMODITY_BRAND"
            )
            s2.data.forEach {
                Log.e(TAG, "商品品牌:$it")
            }

            val r = RetrofitService.instance.getProductById(Robot.accessToken, id)
            Log.e(TAG, "商品详情: $r")

            r.data.companyId?.let {
                Log.e(TAG, "公司信息")
                val company = RetrofitService.instance.getCompanyById(
                    accessToken = Robot.accessToken,
                    companyId = r.data.companyId
                )

                company.data.name?.let {
                    companyName.value = it
                }
                Log.e(TAG, "公司信息2: " + company)
            }
            val listB = r.data.brand?.split(",")?.toList()

            val listA = mutableListOf<Pair<String, String>>();
            s2.data.forEach { item ->
                if (listB != null && listB.contains(item.id)) {
                    listA.add(item.name to item.id)
                }
            }
            _brandList.value = listA
            Log.e(TAG, "商品品牌" + _brandList.value)

            val s3 = RetrofitService.instance.getLabelList(
                accessToken = Robot.accessToken,
                companyId = Robot.companyId,
                type = "COMMODITY_SERIES"
            )
            s3.data.forEach {
                Log.e(TAG, "商品系列:${it}")
            }

            val listC = r.data.series?.split(",")?.toList()
            val tmp = mutableListOf<Pair<String, String>>();
            s3.data.forEach { item ->
                if (listC != null && listC.contains(item.id)) {
                    tmp.add(item.name to item.id)
                }
            }
            _seriesList.value = tmp
            _commodity.value = r.data


            _images.value = r.data.commodityImg?.rotatingImgs.also {
                if (it != null) {
                     all = it.split(",")
                    bigImg.value = all .component1()
                }
            }

            launch {
                while (true){
                    kotlinx.coroutines.delay(2000)
                    _images.value?.let {
                        bigImg.value = all[index%all.size];
                        index++;
                    }
                }
            }

            r.data.commodityImg?.detailImgUrl?.let {
                detailImg.value = it.also {
                    Log.e(TAG, "detailImgUrl:$it")
                }
            }
        }catch (e:Exception){
            e.printStackTrace();
                showToast(e.message!!)
        }
        }
    }
}

